Skip to content

Sanitize the datatable title, study name and related titles on the datatable page - #56

Merged
kf8a merged 4 commits into
masterfrom
fix/escape-datatable-title
Nov 3, 2026
Merged

kf8a merged 4 commits into
masterfrom
fix/escape-datatable-title

Conversation

@kf8a

@kf8a kf8a commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Summary

datatables/show.html.erb printed a datatable's title, its study's name and its related tables' titles with raw, so markup in any of them ran in the page. (The page <title> and breadcrumb already escaped the title.) They now go through a sanitized_title helper:

  • Kept: sub, sup, em, i, strong and b, with no attributes, so a title like N<sub>2</sub>O at 5&deg;C still renders as formatted text. HTML entities display normally.
  • Removed: scripts, links, event handlers, javascript: URLs and every other tag. The text inside is kept.
  • Where: the <h1> (title and study name) and the "Related Tables" links, which stay links.

The helper is a one-line wrapper around Rails' own sanitize (rails-html-sanitizer / Loofah) with a narrow tag list. It isn't a custom sanitizer. It exists so the list is in one place; the default sanitize allowlist is wider than a title needs (links, images, headings, tables).

The branch started as "escape the title" and became "sanitize" after discussion, so it has four commits. Squash on merge if you want one.

Before merging

Titles that deliberately contain other markup, such as links or line breaks, will lose it. I can't see production data; a quick check is:

SELECT id, title FROM datatables WHERE title ~ '[<&]';
SELECT id, name FROM studies WHERE name ~ '[<&]';

Not changed

Still printed with raw on this page: the embedded JSON-LD (line 3) and the Markdown-rendered description (line 27). They hold different content and need a different approach. About 16 other raw/html_safe uses remain in the datatable and subdomain views.

Testing

  • spec/requests/datatable_title_escaping_spec.rb: simple formatting and entities are kept; scripts, event handlers and links are removed from the title; the study name is sanitized; related-table titles are sanitized and stay links. Each fails against the old raw output.
  • Full suite: 316 examples, 0 failures, 3 pending. Run against the schema.rb before 7f502da, since master's current schema can't be loaded into a fresh database (see the structure.sql PR Dump the database schema as db/structure.sql #53).

Generated by Claude Code

The heading printed the title with raw, so markup in a title ran in the
page. The page's <title> tag and breadcrumb already escape it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7
Titles can carry simple formatting (N<sub>2</sub>O), so rather than showing
the tags as text, the heading keeps sub, sup, em, i, strong and b without
attributes and removes everything else, including scripts, links and event
handlers.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7
It sits next to the title in the same heading and was still printed with
raw. It gets the same rules: simple inline formatting kept, everything that
could run removed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7
They were printed with raw inside their links. They get the same rules as
the heading: simple inline formatting kept, everything that could run
removed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7
@kf8a
kf8a merged commit 1901523 into master Nov 3, 2026
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants